

const mongoose = require('mongoose');
var express = require('express')
// 注意这里的声明
var userProfessions= express()

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const { userProfessionService } = require('../../service/impl/userProfessionService/index');
const { locationProfession } = require('../../beans/impl/locationProfession/locationProfession');





//#region  用户职业信息
// 用户给自己添加职业
userProfessions.post('/post/user/profession/add',async (req,res) => {
    let result
    try {
      let conditions = {
        "userid":req.body.conditions.userid,
        // "professions.professionid":req.body.updates.professions.professionid,
        "professions.professionid":req.body.updates.professionid,
        //  "professions.isShow":false
        }
      
      
      let updates = req.body.updates
      let ii = await userProfessionService.getIns().findOne({
        "userid": conditions.userid
      })
      
      if(ii == null || ii == undefined){
        result = await userProfessionService.getIns().create({
          "userid": req.body.conditions.userid,
          "professions": {
            "professionid": req.body.updates.professionid, 
          }
        })
      } else {
        let oo =  await userProfessionService.getIns().findOne({
          "userid": conditions.userid,
          "professions.professionid": req.body.updates.professionid,
        })

        let uu 
      
        if(oo == null || oo == undefined){
          uu= await userProfessionService.getIns().findOneAndUpdate(
            { "userid": conditions.userid },
            { $push: { "professions": { "professionid": req.body.updates.professionid } } },
            { new: true } 
          )

        } else {
          uu = await userProfessionService.getIns().findOneAndUpdate(
            { "userid": conditions.userid, "professions.professionid": req.body.updates.professionid },
            { $set: { "professions.$.isShow": true } },
            { new: true } 
          )
        }

        result = await userProfessionService.getIns().findOne({
          "userid": conditions.userid
        },
        null,
        {
          "populate1": {
            "path": "professions.professionid"
          }
        }
        )
      } 
       
 


  }catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  }
  
  )


  //#region  用户职业信息
// 用户给自己添加职业
// userProfessions.post('/post/user/profession/add',async (req,res) => {
//   let result
//   try {
//     let conditions = {
//       "userid":req.body.conditions.userid,
//       "professions.professionid":req.body.updates.professionid,
//       //  "professions.isShow":false
//       }
    
    
//     let updates = req.body.updates
//     let oo =  await userProfessionService.getIns().findOne(conditions)
//     // let oo =  await userProfessionService.getIns().findOne({"userid":req.body.conditions.userid,"professions.professionid":req.body.updates.professions.professionid})

//     if(oo==null||oo==undefined){
//       result = await userProfessionService.getIns().updateOne(conditions,{$addToSet:updates})
//     }else {
//       result = await userProfessionService.getIns().updateOne(conditions,{$set:{"professions.$.isShow":true}})
//       // res.send(returnDatas(true,result,'该职业已添加到该用户'))
//     }
//   } catch (error) {
//     console.log(error);
//     createResponse(res,500,businessCodes.b2)
//     return
//   }
//     createResponse(res,200,businessCodes.b1,result)
// })


    // A09-根据用户id,查询该用户de 职业信息
    userProfessions.post('/post/user/profession/finduserprofessionInfo',async (req,res) => {
      let result
      try {
        let conditions = {
          "userid":req.body.conditions.userid,
          "professions.isShow":true
        }
        let fills = {
          "populate1":{
            "path":"professions.professionid",
            "populate":{
              "path":"kemuIDArray"
            }
          },
          "populate2":{
            "path":"professions.professionid",
            "populate":{
              "path":"direction_id"
            }
          }
        }
         result =  await userProfessionService.getIns().findOne(conditions,null,fills)
        if(!result){
         result =   await userProfessionService.getIns().create({
            "userid":req.body.conditions.userid
          })
        }
      } catch (error) {
        console.log(error);
        createResponse(res,500,businessCodes.b2)
        return
      }
        createResponse(res,200,businessCodes.b1,result)
    })
  
  // userProfessions.post('/post/user/profession/delete',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findByIdAndRemove(req,res)
  // })
  
  // 用户删除,自己的指定的职业(就是改isshow为false)
  userProfessions.post('/post/user/profession/deleteProUserSelf',async (req,res) => {
    let result
    try {
      let conditions = {
          "userid" :req.body.conditions.userid,
          "professions.professionid" :req.body.conditions.professionid
      }
      let updates ={
        "professions.$.isShow":false
      }
     let oo  =  await userProfessionService.getIns().findOneAndUpdate(conditions,{$set:updates})
     result = await userProfessionService.getIns().findOne({
      "userid": conditions.userid
    },
    null,
    {
      "populate1": {
        "path": "professions.professionid"
      }
    }
    )
      
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
   })
  
  // userProfessions.post('/post/user/profession/update',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findByIdAndUpdate(req,res)
  // })
  
  // userProfessions.post('/post/user/profession/findByObjAndUpdate',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findByObjAndUpdate(req,res)
  // })
  

  
  //用户根据职业id,设置该职业为自己的默认做题职业
  userProfessions.post('/post/user/profession/findByObjAndUpdate/update_default_profession',async (req,res) => {
    let result
    let result2
    try {
      let conditions1 = {
        "userid":req.body.conditions.userid,
        "professions.default_title_profession":true
      }

      let update1={
        $set: { 'professions.$.default_title_profession': false }
      }

      let conditions = {
        "userid":req.body.conditions.userid,
        "professions.professionid":req.body.conditions.professionid
      }

      let update ={
        $set: { 'professions.$.default_title_profession': true }
      }
      // 把原来的第一职业修改为false
        result=    await userProfessionService.getIns().findOneAndUpdate(conditions1,update1)
      //  设置新的第一职业
        result2=    await userProfessionService.getIns().findOneAndUpdate(conditions,update)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
  })
  
  // A08-根据用户id,获取用户的默认刷题职业,包含默认刷题科目
  // userProfessions.post('/post/user/profession/findByObjAndUpdate/findeaul',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findByObj(req,res)
  // })
  
  // A08-根据用户id,查询该用户的默认刷题职业
  // userProfessions.post('/post/user/profession/findByObjAndUpdate/finddefaultprofession',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findByObj(req,res)
  // })
  

  
  
  // 根据科目id-用户设置该职业的默认做题科目
  // userProfessions.post('/post/user/profession/findByObjAndUpdate/set_default_kemu',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findByObjAndUpdate(req,res)
  // })
  
  // A05用户设置自己的职业排序,如第1职业
  // userProfessions.post('/post/user/profession/findByObjAndUpdate/set_profession_order',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findByObjAndUpdate(req,res)
  // })
  
  // 根据用户id,查询该用户的职业信息
  // userProfessions.post('/post/user/profession/finduserprofessionByUserid',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findOneByObjAndPopulate(req,res,'professions.professionid')
  // })
  
  
  //A11-设置默认刷题职业的,默认做题科目
  // userProfessions.post('/post/user/profession/findByObjAndUpdate/set_default_kemu_by_default_pro',(req,res) => {
  //   controller.user_location_profession_controller.getSigleIns().findByObjAndUpdate(req,res)
  // })
  
  
  
  //#endregion
  

  module.exports= userProfessions